$(function () {
    let markId = getUrlString('markid');
    $('#close').click(function () {
        nativeCommand('dancool://activity=fin');
    });
    let pics = ['https://img1.odancool.com/BQB001.gif',
        'https://img1.odancool.com/BQB002.gif',
        'https://img1.odancool.com/BQB003.gif',
        'https://img1.odancool.com/BQB004.gif',
        'https://img1.odancool.com/BQB005.gif',
        'https://img1.odancool.com/BQB006.gif',
        'https://img1.odancool.com/BQB007.gif',
        'https://img1.odancool.com/BQB008.gif',
        'https://img1.odancool.com/BQB009.gif',
        'https://img1.odancool.com/BQB010.gif',
        'https://img1.odancool.com/BQB011.gif',
        'https://img1.odancool.com/BQB012.gif',
        'https://img1.odancool.com/BQB013.gif',
        'https://img1.odancool.com/BQB014.gif',
        'https://img1.odancool.com/BQB015.gif',
        'https://img1.odancool.com/BQB016.gif',
        'https://img1.odancool.com/BQB017.gif',
        'https://img1.odancool.com/BQB018.gif',
        'https://img1.odancool.com/BQB019.gif',
        'https://img1.odancool.com/BQB020.gif',
        'https://img1.odancool.com/BQB021.gif',
        'https://img1.odancool.com/BQB022.gif',
        'https://img1.odancool.com/BQB023.gif',
        'https://img1.odancool.com/BQB024.gif',
        'https://img1.odancool.com/BQB025.gif',
        'https://img1.odancool.com/BQB026.gif',
        'https://img1.odancool.com/BQB027.gif',
        'https://img1.odancool.com/BQB028.gif',
        'https://img1.odancool.com/BQB029.gif',
        'https://img1.odancool.com/BQB030.gif',
        'https://img1.odancool.com/BQB031.gif',
        'https://img1.odancool.com/BQB032.gif',
        'https://img1.odancool.com/BQB033.gif',
        'https://img1.odancool.com/BQB034.gif',
        'https://img1.odancool.com/BQB035.gif',
        'https://img1.odancool.com/BQB036.gif',
        'https://img1.odancool.com/BQB037.gif',
        'https://img1.odancool.com/BQB038.gif',
    ];
    let append = '';
    for (let i=0;i<pics.length;i++){
        append += '<img src="'+pics[i]+'" alt="">'
    }
    $('.pics').append(append);
    $('.pics img').click(function () {
        let size = $("#picList").children().length;
        console.log(size)
        if (size > 9){
            laymsg('最多上传9张图片');
            $('#addPic').hide();
        }else {
            $('#addPic').show();
            let ccc = $(this).attr('src');
            let text='<li>' +
                '            <img class="image" src="'+ccc+'" alt="">' +
                '            <img class="closeAlert" src="../../res/img/common/delete.png" alt="">' +
                '        </li>';
            $('#picList').prepend(text);
            let size2 = $("#picList").children().length;
            if (size2 >= 10){
                $('#addPic').hide();
            }else {
                $('#addPic').show();
            }
            // var size = $("#picList").children().length;
            // console.log(size)
            $('#picList li .closeAlert').click(function () {
                $(this).parent().remove();
                let size = $("#picList").children().length;
                console.log(size)
                if (size < 10){
                    $('#addPic').show();
                }
            })
        }
    });
    $('.bqBtn').click(function () {
        $('.bottomTag').hide();
        $('.bottomPics').show()
    });
    $('.closePics').click(function () {
        $('.bottomPics,.bottomTag').hide()
    });
    $('.tagBtn').click(function () {
        $('.bottomPics').hide();
        $('.bottomTag').show()
    });
    let isajax = true;
    function release () {
        isajax = false;
        let intro = $('#releaseContent').val();
        //所有图片
        let my_imgs = $("#picList").find(".image");
        let picurls="";
        my_imgs.each(function(index,e){
            console.log('pics['+index+']='+$(e).attr("src"));
            if(picurls === ""){
                picurls=""+$(e).attr("src");
            }else{
                picurls = picurls+";"+$(e).attr("src");
            }
        });
        if (intro === ''){
            laymsg('内容不能为空');
            isajax = true;
            return
        }
        if (intro.length < 3){
            laymsg('发帖内容过少');
            isajax = true;
            return
        }
        let data = {
            'markid':markId,
            'type':'0',
            'intro':intro,
            'picurls':picurls,
        };
        getData('post','/niu/apiv3/weibo_dopost.php',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                laymsg(data.msg);
                getShare();
                // tag();
                // hudong();
                setTimeout(function () {
                    nativeCommand('dancool://activity=reflashfin');
                },2000)
            } else {
                isajax = true;
                laymsg(data.msg)
            }
        })
    }

    function getTag() {
        getData('post','/niu/apiv4/niu_tag_info.php',{'markid':markId,},function (data) {
            console.log(data);
            let append = '';
            $(data.tagInfo).each(function () {
                append += '<li>#'+this.talk_name+'#</li>'
            });
            $('.tagList').append(append);
            $('.tagList li').click(function () {
                let val = $('#releaseContent').val();
                let html = $(this).html();
                let value = val +''+ html;
                $('#releaseContent').val(value)
            })
        })
    }
    getTag();

    function getShare () {
        getData('post','/niu/apiv4/niu_duanwu_huodong.php',{'markid':markId,'method':'Fa_Tie'},function (data) {
            console.log(data)
        });
    }

    function  tag () {
        getData('post','/niu/apiv4/niu_tag_gift.php',{'markid':markId,},function (data) {
            // console.log(data);
        })
    }

    function  hudong  () {
        getData('post','/niu/apiv4/niu_wuyi_hudong.php',{'markid':markId,'method':'Fa_Tie'},function (data) {
            // console.log(data);
        })
    }

    $('#releaseBtn').click(function () {
        if (isajax){
            release()
        }
    });

    $('.upLoad').click(function () {
        uploadpics();
    });

    function uploadpics() {
        //查看已经有的图片数，超过9个不再上传
        var size=$("#picList").children().length;
        var left=10 - size;
        //如果有N小于9个，则可一上传9-N个
        //调用本地接口
        // console.log("left uploadimg=" + left);
        if(left>0){
            getnative("dancool://uploadfilev2="+left+";700");
        }else {
            laymsg('最多上传9张图片')
        }
    }



    function getnative(actionurl) {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isiOS) {
            window.webkit.messageHandlers.getnative.postMessage(actionurl);
        }else {
            ikags.getnative(actionurl)
        }
    }
});

function jsnativeresult (aaa,bbb,ccc) {
    console.log('aaa'+aaa);
    console.log('bbb'+bbb);
    console.log('ccc'+ccc);
    if (ccc !== '' || ccc !== null){
        let cccArr = ccc.split(';');
        let text = '';
        for (let i = 0;i < cccArr.length;i++){
             text += '<li>' +
                '            <img class="image" src="'+cccArr[i]+'" alt="">' +
                '            <img class="closeAlert" src="../../res/img/common/delete.png" alt="">' +
                '        </li>';
        }

        $('#picList').prepend(text);
        let size2 = $("#picList").children().length;
        if (size2 >= 10){
            $('#addPic').hide();
        }else {
            $('#addPic').show();
        }
        $('#picList li .closeAlert').click(function () {
            $(this).parent().remove();
            let size = $("#picList").children().length;
            if (size < 10){
                $('#addPic').show();
            }
        })
    }

}
